﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using TalentPools.Model;
using TalentPools.BLL;
namespace WebManager
{
    /// <summary>
    /// 页面基类,所有页面都应继承此类
    /// </summary>
    public class BasePage : System.Web.UI.Page
    {
        Admin_LoginManager admin = new Admin_LoginManager();
        public BasePage()
        {
            this.PreInit += new EventHandler(BasePage_PreInit);
        }
        void BasePage_PreInit(object sender, EventArgs e)
        {

            string name = Request["username"];
            Response.Write(name);
            if (Request["t"] == "t")
            {
                Response.Write("<script>alert('" + DateTime.Now + ":这是一个测试信息！来着BasePage的PreInit')</script>");
            }
        }
        public string CutString(string str, int len)
        {
            return str.Substring(0, len) + "...";
        }
        public static string SetTimeFormat(DateTime datetime)
        {
            string year = datetime.Year.ToString();
            string month = datetime.Month.ToString();
            string day = datetime.Day.ToString();
            return year + "年" + month + "月" + day + "日";
        }

        public static string SetSex(object sex)
        {
            if (Convert.ToBoolean(sex))
            {
                return "男";
            }
            else
            {
                return "女";
            }
        }
        public static string SetMarriage(object marriage)
        {
            if (marriage.ToString() == "401")
            {
                return "未婚";
            }
            else if (marriage.ToString() == "402")
            {
                return "已婚";
            }
            else
            {
                return "保密";
            }
        }

        public static string SetPoliState(object p)
        {
            string pstr = p.ToString();
            if (pstr == "")
            {
                return "未知";
            }
            else
            {
                return pstr;
            }
        }

        public static string SetHKPlace(object hk)
        {
            string hkStr = hk.ToString();
            if (hkStr == "")
            {
                return "户籍不详";
            }
            else
            {
                return hkStr;
            }
        }

        public static string SetCity(object c)
        {
            string cStr = c.ToString();
            if (cStr == "")
            {
                return "地址不详";
            }
            else
            {
                return cStr;
            }
        }
        public static String SetEmail(object e)
        {
            string eStr = e.ToString();
            if (eStr == "")
            {
                return "邮箱地址不详";
            }
            else
            {
                return eStr;
            }
        }

        public static string SetPhone(object p)
        {
            string pStr = p.ToString();
            if (pStr == "")
            {
                return "手机号码不详";
            }
            else
            {
                return pStr;
            }
        }

        public static string SetCard(object c)
        {
            string cStr = c.ToString();
            if (cStr == "")
            {
                return "身份证不详";
            }
            else
            {
                return cStr;
            }
        }

        #region 弹出JS窗口提示框
        /// <summary>
        /// 弹出JS窗口提示框
        /// </summary>
        /// <param name="message">提示消息</param>
        /// <param name="url">URL地址，可选参数，为空则只弹出对话框，而不刷新页面</param>
        public void JsWindows(string message, string url)
        {
            if (url == "")
            {
                HttpContext.Current.Response.Write("<script>alert('" + message + "')</script>");
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert('" + message + "');window.location='" + url + "'</script>");
            }
        }
        #endregion

        #region 判断企业用户是否登录
        /// <summary>
        /// 判断企业用户是否登录
        /// </summary>
        public bool IsQyLogin()
        {
            if (Request.Cookies["qyUser"] == null || (Request.Cookies["qyUser"].Values["key"] != FormatCookie(Server.UrlDecode(Request.Cookies["qyUser"].Values["name"]))))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion

        #region 判断个人用户是否登录
        /// <summary>
        /// 判断个人用户是否登录
        /// </summary>
        public bool MyUserLogin()
        {
            if (Request.Cookies["User"] == null || (Request.Cookies["User"].Values["key"] != FormatCookie(Server.UrlDecode(Request.Cookies["User"].Values["name"]))))
            {
                return false;
            }
            return true;
        }
        #endregion

        #region 判断管理员是否登录
        /// <summary>
        /// 判断管理员是否登录
        /// </summary>
        public bool AdminLogin()
        {
            if (Request.Cookies["AdminUser"] == null || (Request.Cookies["AdminUser"].Values["key"] != FormatCookie(Request.Cookies["AdminUser"].Values["name"])))
            {
                return false;
            }
            return true;
        }
        #endregion

        #region 格式化文本（防止SQL注入）
        /// <summary>
        /// 格式化文本（防止SQL注入）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string MyFormatstr(string html)
        {
            System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex10 = new System.Text.RegularExpressions.Regex(@"select", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex11 = new System.Text.RegularExpressions.Regex(@"update", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex12 = new System.Text.RegularExpressions.Regex(@"delete", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            html = regex1.Replace(html, ""); //过滤<script></script>标记
            html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
            html = regex4.Replace(html, ""); //过滤iframe
            html = regex5.Replace(html, ""); //过滤frameset
            //html = regex6.Replace(html, ""); //过滤frameset
            //html = regex7.Replace(html, ""); //过滤frameset
            //html = regex8.Replace(html, ""); //过滤frameset
            //html = regex9.Replace(html, "");
            html = regex10.Replace(html, "s_elect");
            html = regex11.Replace(html, "u_pudate");
            html = regex12.Replace(html, "d_elete");
            html = html.Replace("'", "’");
            html = html.Replace("&nbsp;", " ");
            //html = html.Replace("</strong>", "");
            //html = html.Replace("<strong>", "");
            return html;
        }
        #endregion

        #region 个人用户登录，存入Cookie
        /// <summary>
        /// 个人用户登录，存入Cookie
        /// </summary>
        /// <param name="name"></param>
        public void UserCookie(string name)
        {
            HttpCookie userCookie = new HttpCookie("User");
            if (HttpContext.Current.Request.Cookies["User"] != null)
            {
                userCookie.Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Cookies.Add(userCookie);
            }
            userCookie["name"] = MyFormatstr(Server.UrlEncode(name));
            userCookie["key"] = FormatCookie(name);
            userCookie.Expires = DateTime.Now.AddMinutes(30);
            HttpContext.Current.Response.Cookies.Add(userCookie);
        }
        #endregion

        #region 管理员登录，存入Cookie
        /// <summary>
        /// 管理员登录，存入Cookie
        /// </summary>
        /// <param name="name"></param>
        public void AdminUser(string name)
        {
            HttpCookie AdminUser = new HttpCookie("AdminUser");
            if (HttpContext.Current.Request.Cookies["AdminUser"] != null)
            {
                AdminUser.Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Cookies.Add(AdminUser);
            }
            AdminUser["name"] = MyFormatstr(Server.UrlEncode(name));
            AdminUser["key"] = FormatCookie(name);
            AdminUser.Expires = DateTime.Now.AddMinutes(30);
            HttpContext.Current.Response.Cookies.Add(AdminUser);
        }
        #endregion

        #region 企业用户登录，存入Cookie
        /// <summary>
        /// 企业用户登录，存入Cookie
        /// </summary>
        /// <param name="name"></param>
        public void qyCookie(string name)
        {
            HttpCookie qyCookie = new HttpCookie("qyUser");
            if (HttpContext.Current.Request.Cookies["qyUser"] != null)
            {
                qyCookie.Expires = DateTime.Now.AddHours(-1);
                HttpContext.Current.Response.Cookies.Add(qyCookie);
            }
            qyCookie["name"] = MyFormatstr(Server.UrlEncode(name));
            qyCookie["key"] = FormatCookie(name);
            qyCookie.Expires = DateTime.Now.AddMinutes(30);
            HttpContext.Current.Response.Cookies.Add(qyCookie);
        }
        #endregion

        #region 加密Cookie
        /// <summary>
        /// 加密Cookie
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string FormatCookie(string name)
        {
            string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            string agent = HttpContext.Current.Request.UserAgent;
            string key = HttpContext.Current.Application["sitekey"].ToString();

            string c_key = FormsAuthentication.HashPasswordForStoringInConfigFile(ip + agent + key + name, "MD5");
            return c_key;
        }
        #endregion

        #region 获取HTML的参数
        /// <summary>
        /// 获取HTML的参数
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public string QueryString(string s)
        {
            if (Request.QueryString[s] != null)
            {
                return MyFormatstr(Request.QueryString[s].ToString());
            }
            return string.Empty;
        }
        #endregion

        

    }
}
